package com.disc.controller;

import com.common.GeetestConfig;
import com.common.GeetestLib;
import com.common.User;
import com.disc.baidu.BaiduUser;
import com.disc.common.DiscResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class CommonController {
    @RequestMapping("login")
    public ModelAndView gotoLoginPage()
    {
        return new ModelAndView("login");
    }

    @RequestMapping("startCaptcha")
    @ResponseBody
    public String startCaptcha(HttpServletRequest request)
    {
        GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(),GeetestConfig.isnewfailback());
        //自定义userid
        String userid = "lucky_search";
        //进行验证预处理
        int gtServerStatus = gtSdk.preProcess(userid);
        //将服务器状态设置到session中
        request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
        //将userid设置到session中
        request.getSession().setAttribute("userid", userid);
        return gtSdk.getResponseStr();
    }

    @RequestMapping("loginSystem")
    public @ResponseBody DiscResponse login(User user,HttpSession session,HttpServletRequest request)
    {

        DiscResponse discResponse = new DiscResponse();

        GeetestLib gtSdk = new GeetestLib(GeetestConfig.getGeetest_id(), GeetestConfig.getGeetest_key(),
                GeetestConfig.isnewfailback());

        String challenge = request.getParameter(GeetestLib.fn_geetest_challenge);
        String validate = request.getParameter(GeetestLib.fn_geetest_validate);
        String seccode = request.getParameter(GeetestLib.fn_geetest_seccode);

        //从session中获取gt-server状态
        int gt_server_status_code = (Integer) request.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);

        //从session中获取userid
        String userid = (String)request.getSession().getAttribute("userid");

        int gtResult = 0;

        if (gt_server_status_code == 1) {
            //gt-server正常，向gt-server进行二次验证

            gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, userid);
            System.out.println(gtResult);
        } else {
            // gt-server非正常情况下，进行failback模式验证

            System.out.println("failback:use your own server captcha validate");
            gtResult = gtSdk.failbackValidateRequest(challenge, validate, seccode);
            System.out.println(gtResult);
        }

        if (gtResult == 1)
        {
            // 验证成功
            if(user != null && "admin".equals(user.getAccount()) && "admin".equals(user.getPassword()))
            {
                session.setAttribute("user",user);
                discResponse.setCode("0");
                discResponse.setMessage("登录成功！");
            }
            else
            {
                discResponse.setCode("-1");
                discResponse.setMessage("账号或者密码错误！");
            }
        }
        else {
            // 验证失败
            discResponse.setCode("-1");
            discResponse.setMessage("验证码错误！");
        }
        return discResponse;
    }

}
